const db = require('../db/index')
const bcrypt = require('bcryptjs')
const jwt=require('jsonwebtoken')
const config=require('../config')


exports.reqLogin = (req, res) => {
    let { username, password } = req.body
    const sql = 'SELECT * FROM user WHERE name=? AND pwd=?';
    db.query(sql, [username,password], (err, results) => {
        if (err) {
            return res.cc(err);
        }
        if (results.length !== 1) {
            return res.cc('登录失败');
        }
        if(results[0].status===0){
            return res.cc('此账号被禁用,请联系管理员');
        }
        let uid = results[0].id;
        const tokenStr = jwt.sign({ uid }, config.jwtSecretKey, { expiresIn: config.expiresIn });
        res.send({
            status: 200,
            msg: '登录成功',
            token: 'Bearer ' + tokenStr,
            name:results[0].name,
            url:results[0].url
        });
    });
};
